<?php
Yii::import('dao.GetEndHistoryInfo');

class IndexaController extends Controller
{

    public $categoryLimit = 10;

    public function init()
    {
        $this->layout = '//layouts/default';
        parent::init();
        $this->http();
    }

    public function actionCategory()
    {
        $id = Yii::app()->request->getParam('category_id', 0);
        if (empty($id)) {
            throw new CHttpException(404, Yii::t('yii', 'category  not.'));
        }
        $category_id = array();
        if ($id === 'live') {
            $categoryTitle = 'Live Auctions';
        }elseif ($id === 'feature'){
        	$categoryTitle='Featured Auctions';
        } elseif ($id === 'closed'){
            $categoryTitle='Closed Auctions';
        }
        
        else {
            $category = Category::model()->findByPk($id);
            if (empty($category)) {
                throw new CHttpException(404, Yii::t('yii', 'category  not.'));
            }
            $categoryTitle = $category->title;
            $category_category_relations = new CategoryCategoryRelations();
            $category_id = $category_category_relations->getAllCategory($id);
        }
        $this->breadcrumbs = array(
             $categoryTitle
        );
        $closeAuctionLimit=10;
        if ($id === 'closed'){
            $model=new AuctionInfo();
            $model->limit=$closeAuctionLimit;
            $model->offset=!empty($_GET['AuctionPoolProvider_page'])?($_GET['AuctionPoolProvider_page']-1)*$model->limit:null;
            $model=$model->search_closed();
        }
        else{
            $model = new AuctionPoolProvider("search");
            
            $model->search_category_id = $id;
            $model->category_id_arr = $category_id;
            $model->limit = $this->categoryLimit;
            if (! empty($_POST['pages'])) {
                $this->categoryLimit = $_POST['pages'];
                $model->limit = $this->categoryLimit;
                $this->layout = false;
            }
            if (!empty($_GET['AuctionPoolProvider_page'])) {
                $model->offset=($_GET['AuctionPoolProvider_page']-1)*$this->categoryLimit;
            }            
            
            if (!empty($_GET['AuctionPoolProvider_limit'])) {
                if (is_array($_GET['AuctionPoolProvider_limit'])){
                    $model->limit=$_GET['AuctionPoolProvider_limit'][0];
                }else{
                    $model->limit=$_GET['AuctionPoolProvider_limit'];
                }
                $closeAuctionLimit=$model->limit;
            }
            $model = $model->search();
        }
        $this->render('category', array(
            'model' => $model,
            'categoryTitle' => $categoryTitle,
            'limit' => $this->categoryLimit
        ));
    }

    public function actionDetail()
    {
        $id = Yii::app()->request->getParam('auction_id', 0);
        if (empty($id)) {
            throw new CHttpException(404, Yii::t('yii', 'auction not.'));
        }
        Yii::import('dao.auction.AuctionDetailDao');
        $auctionDao = new AuctionDetailDao();
        $detail = $auctionDao->find($id);
        $auctionTitle = $detail->title;
        if (empty($auctionTitle)) {
            throw new CHttpException(404, Yii::t('yii', 'auction not.'));
        }
        /* $this->breadcrumbs = array(
            $detail->category_title,
            $detail->title
        ); */

        if ($detail->status!=2) {
        	$auctionPool=new AuctionPool();
        	$auctionPool=$auctionPool->findByAttributes(array('auction_id'=>$detail->id));
        	if (!empty($auctionPool)) {
        		$detail->winner_price=$auctionPool->winner_price;
        	}
        }
        
        $costBids=0;
        $bids=0;
        if (!Yii::app()->user->isGuest) {
        	$customerId=Yii::app()->user->id;
        	$sql='SELECT SUM(`bid_num`) as bids from customer_bid_info WHERE customer_id='.$customerId;
        	$customerBidInfo=Yii::app()->db->createCommand($sql)->queryRow();
        	if (!empty($customerBidInfo)) {
        		$bids=$customerBidInfo['bids'];
        	}
        	
        	$cost=new CustomerBidAuctionCount();
        	$costBidsSql='SELECT SUM(bid_num) as costbids
        					 from customer_bid_auction_count WHERE auction_id='.$detail->id.'
        					AND customer_id='.$customerId.'
        				 ';
        	$costBidResult=Yii::app()->db->createCommand($costBidsSql)->queryRow();
        	if (!empty($costBidResult)) {
        		$costBids=$costBidResult['costbids'];
        	}
        }
        
        $payment=false;
        if (!Yii::app()->user->isGuest && $detail->status==2) {
        	$customerId=Yii::app()->user->id;
        	$auctionInfo=new AuctionInfo();
        	$auctionInfo=$auctionInfo->findByPk($detail->id);
        	if (!empty($auctionInfo) && $auctionInfo->winner_id==$customerId) {
        		$order=new Orders();
        		$order=$order->findByAttributes(array(
        			'customer_id'	=>$customerId,
        			'auction_id' 	=>$detail->id,
        			'type'  		=>1
        		));	
        		if (!empty($order)) {
        			$payment=true;
        		}
        	}
        }
        
        $model = new AuctionPoolProvider();
        $model->not_ids = $id;
        $model->category_id_arr=array($auctionDao->category_id);
        $model->limit = 3;
        $model = $model->search();

        
        $auctionBidLogCDbCriteria = new CDbCriteria();
        $auctionBidLogCDbCriteria->compare('auction_id', $id);
        $auctionBidLogCDbCriteria->order = ' winner_price DESC';
        $auctionBidLogCDbCriteria->limit = 10;
        
        
        $auctionBidLog = new AuctionBidLog();
        $auctionBidLog = $auctionBidLog->findAll($auctionBidLogCDbCriteria);
        
        $winnerBids=0;
        if ($detail->status==2){
            $winnerBids=new AuctionBidLog();
            $auctionDetail=new AuctionInfo();
            $auctionDetail=$auctionDetail->findByPk($id);
            $winnerBids=$winnerBids->countByAttributes([
                "auction_id"=>$id,
                "customer_id"=>$auctionDetail->winner_id,
            ]);
        }
        
        $this->render('detail', array(
            'detail' 		=> $detail,
            'model' 		=> $model,
            'bidHistory' 	=> $auctionBidLog,
        	'bids'			=>$bids,
        	'costbids'		=>$costBids,
        	'payment'		=>$payment,
            'winnerBids'=>$winnerBids
        ));
    }

    public function actionIndex()
    {
    	$category_id = array();
    	$featureModel=new AuctionPoolProvider();
    	$liveModel=new AuctionPoolProvider();
    	$liveModel->limit=10;
    	
    	/**
    	 * 判断用户是否关闭新手帮助banner
    	 */
    	$closeNewuserIns=false;
    	if (!Yii::app()->user->isGuest){
        	$customer=new CustomerInfo();
           	$customer=$customer->findByAttributes(array(
           	    'customer_id'=>Yii::app()->user->id,
           	    'close_newuser_ins'=>1
           	));
           	$closeNewuserIns=empty($customer)?$closeNewuserIns:$customer->close_newuser_ins;
    	}
    	
    	$featureModel->search_category_id = 'feature';
    	
    	$featureModel->category_id_arr =$liveModel->category_id_arr= $category_id;
    	
    	$featureModel = $featureModel->search();
    	
    	$liveModel=$liveModel->search();
    	$this->render('indexa', array(
            'featureModel' => $featureModel,
    		'liveModel'	   =>$liveModel,
    	    'closeNewuserIns'=>$closeNewuserIns
        ));
    }

    /*
     * 单页部分
     */
    public function actionPrivacy()
    {
        $this->breadcrumbs = array(
            "privacy"
        );
        $this->render('privacy');
    }

    public function actionTermsandConditions()
    {
        $this->breadcrumbs = array(
            "Terms and Conditions"
        );
        
        $this->render('TermsandConditions');
    }

    public function actionHowitworks()
    {
        $this->breadcrumbs = array(
            "How it works"
        );
        
        $this->render('Howitworks');
    }

    public function actionAboutEgunner()
    {
        $this->breadcrumbs = array(
            "About Egunner"
        );
        
        $this->render('AboutEgunner');
    }
    public function actionFaq()
    {
        $this->breadcrumbs = array(
            "FAQ"
        );
    
        $this->render('faq');
    }

    public function actionReturnsandConcellations()
    {
        $this->breadcrumbs = array(
            "Returns and Concellations"
        );
        
        $this->render('ReturnsandConcellations');
    }
    
    /**
     * 在主站显示feature auction
     */
    public function actionFeaturedAuctions(){
        $this->layout="nolayout";
        
        $featureModel=new AuctionPoolProvider();
        
        $featureModel->search_category_id = 'feature';
        
        
        $featureModel->limit=16;
         
        //$featureModel->category_id_arr = $category_id;
         
        $featureModel = $featureModel->searchForFeature();
        
        $this->render("featuredauctions",array(
            'featureModel' => $featureModel,
        ));
    }
}